#!/usr/bin/env bash

#sed -i 's#archive.ubuntu.com#maven.saas.crland.com.cn/nexus/repository/aliyun-mirrors#g' /etc/apt/sources.list \
#    && sed -i 's#security.ubuntu.com#maven.saas.crland.com.cn/nexus/repository/aliyun-mirrors#g' /etc/apt/sources.list \
#    && sed -i 's#mirrors.ustc.edu.cn#maven.saas.crland.com.cn/nexus/repository/aliyun-mirrors#g' /etc/apt/sources.list
#
CONFIG_PATH=/config/config-"$APP_ENV".ini
# 安装 tzdata 软件包并设置时区
ln -snf /usr/share/zoneinfo/"$TZ" /etc/localtime && \
    echo "$TZ" > /etc/timezone
#echo "替换环境文件$CONFIG_PATH 至/opt/media/bin/config.ini,日志级别：$LOG_LEVEL"
cp "$CONFIG_PATH" /opt/media/bin/config.ini

sed -i "s/mediaServerId=.*$/mediaServerId=$SERVERID/g" /opt/media/bin/config.ini

# 根据pod副本数动态拼K8S内部访问域名封装为origin_url并替换文件内的默认值
index=$((POD_NUM))
NumList=$(seq $index)

result=""
i=0
until [ $i -ge $index ]; do
    result="$result${TRACE_TYPE}:\/\/${SERVICE}-0.${SERVICE}:"${PORT}"\/%s\/%s;"
#    result="${TRACE_TYPE}:\/\/${SERVICE}-0.${SERVICE}:"${PORT}"\/%s\/%s;"
    i=$((i+1))
done
sed -i "s/origin_url=/origin_url=$result/g" /opt/media/bin/config.ini
sed -i "s/host.docker.internal:8844/$WEBHOOK/g" /opt/media/bin/config.ini

cd /opt/media/bin
cat config.ini
# 查看工作目录下的文件
ls -l
# 执行传入的CMD参数
exec "$@"